const MenuUtils = {
    getTreeItemByUrl(dataList, url) {
        for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].url === url) {
                return dataList[i]
            }
            if (dataList[i].children && dataList[i].children.length > 0) {
                const result = this.getTreeItemByUrl(dataList[i].children, url)
                if (result) {
                    return result
                }
            }
        }
        return null
    },
    getTreeItem(dataList, id) {
        for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].id === id) {
                return dataList[i]
            }
            if (dataList[i].children && dataList[i].children.length > 0) {
                const result = this.getTreeItem(dataList[i].children, id)
                if (result) {
                    return result
                }
            }
        }
        return null
    },
    getUrlPath(treeList, url) {
        const result = []
        let item = this.getTreeItemByUrl(treeList, url)
        if (!item) {
            return
        }
        let parentId = item.parentId
        while (parentId) {
            item = this.getTreeItem(treeList, parentId)
            result.push(item.id)
            parentId = item.parentId
        }
        return result.reverse()
    },
    getTreeNode(treeList, url) {
        return this.getTreeItemByUrl(treeList, url)
    }
}

export default MenuUtils
